<?php
    namespace App\Models;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Database\Eloquent\Model;
    //推荐人
    class RecommandUser extends Model{
        protected $table = 'waihui_tuijian_user';
       
        
        /* 判断tuijian_user表是否存在
         * @param userid int 用户id
         * @return 
         * @author zhaoruijuan
         */
        public function isSetNickName($userid){
           $userData = $this->where('userid',$userid)->first();
           return $userData;
        }
        
        
        /*分析师历史发布的榜单列表
         * @param $id int waihui_tuijain_user 的id
         * @return  
         */
        public function history($userid,$pagesize){    
           $userData = $this->isSetNickName($userid);
           $lists = $this->find($userData['id'])->RecommandList()->select('*','waihui_tuijian_list.id as listid')->where([["waihui_tuijian_list.status",'=',1]])->orderBy('time','desc')->paginate($pagesize);
           $lists->withPath('hrecommand?id='.$userid);
           return $lists;
        }
        
        public function RecommandList(){
          return $this->hasManyThrough('App\Models\RecommandList','App\Models\RecommandUser' ,'id','tuijian_id');
        } 
        
        /*判断昵称是否已经被占用
         * @param $nickname string
         * @return int
         */
        public function checkNameHasExist($nickname){
            $isExist = $this->where('nickname',$nickname)->first();
            if(count($isExist) > 0){
                return $isExist['userid']; //已被占用,返回用户id
            }else{
                return 1; //未被占用
            }
        }
        
    } 
?>    
    

